package business;

import java.util.Date;
import javax.ejb.Stateless;
import business.histocloud.Histogram;
import business.histocloud.LatentSemantic;
import business.histocloud.TagCloud;
import business.histocloud.component.Article;
import business.histocloud.component.Word;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.imageio.ImageIO;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;

@Stateless

public class Visualiser {

    public static final int LEMONDE = 1;
    public static final int LEFIGARO = 2;
    public static final int LIBERATION = 3;

    LatentSemantic latentSemantic;
    static final String IMAGE_PATH = "F:\\analyse\\visual\\";

    public void createImage(Date from, Date to, int threshold) throws Exception {
        //get number of day from T1 to T2;      

        int numberOfArticles = 1;
        //get all stemmed articles
        Article[] articleLemonde = new Article[1];
        Article[] articleFigaro = new Article[1];
        Article[] articleLiber = new Article[1];

        for (int i = 0; i < numberOfArticles; i++) {
            articleLemonde[i] = getWordIn(from, to, LEMONDE, threshold);
            articleFigaro[i] = getWordIn(from, to, LEFIGARO, threshold);
            articleLiber[i] = getWordIn(from, to, LIBERATION, threshold);
        }

        if (!articleLemonde[0].words.isEmpty() && !articleFigaro[0].words.isEmpty() && !articleLiber[0].words.isEmpty()) {
            
            //initial tagclouds and histograms of each journal
            TagCloud tagCloudLemonde = new TagCloud(articleLemonde, threshold);
            Histogram histoLemonde = new Histogram(articleLemonde, threshold);
            TagCloud tagCloudFigaro = new TagCloud(articleFigaro, threshold);
            Histogram histoFigaro = new Histogram(articleFigaro, threshold);
            TagCloud tagCloudLiber = new TagCloud(articleLiber, threshold);
            Histogram histoLiber = new Histogram(articleLiber, threshold);
            latentSemantic = new LatentSemantic(histoLemonde.getWordWithValue(), histoFigaro.getWordWithValue(), histoLiber.getWordWithValue(), "lemonde", "lefigaro", "liberation");
            
            //write these images to disk
            try {
                ImageIO.write(tagCloudLemonde.getImage(), "png", new File(IMAGE_PATH + "c_lemonde.png"));
                ImageIO.write(histoLemonde.getImage(), "png", new File(IMAGE_PATH + "h_lemonde.png"));
                ImageIO.write(tagCloudFigaro.getImage(), "png", new File(IMAGE_PATH + "c_figaro.png"));
                ImageIO.write(histoFigaro.getImage(), "png", new File(IMAGE_PATH + "h_figaro.png"));
                ImageIO.write(tagCloudLiber.getImage(), "png", new File(IMAGE_PATH + "c_liberation.png"));
                ImageIO.write(histoLiber.getImage(), "png", new File(IMAGE_PATH + "h_liberation.png"));
                ImageIO.write(latentSemantic.getIsometricImage(), "png", new File(IMAGE_PATH + "isometric.png"));
                ImageIO.write(latentSemantic.getPlanImage(), "png", new File(IMAGE_PATH + "plan.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            throw new Exception();
        }
    }

    //get P value of Chi-Square test
    public double getChiSquareTestValue() {
        if (latentSemantic != null) {
            return latentSemantic.getX2Statistic();
        } else {
            return 0;
        }
    }

    private Article getWordIn(Date from, Date to, int journal, int threshold) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String fromDate = dateFormat.format(from);
        String toDate = dateFormat.format(to);
        ClientConfig config = new ClientConfig();
        Client client = ClientBuilder.newClient(config);
        
        //call webservice to get words
        WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8080/AnalyseJournauxRESTfulAPI/myresource/getWords?from=" + fromDate + "&to=" + toDate + "&type=" + journal + "&maxNumber=" + threshold).build());
        Word[] words = target.request().accept(MediaType.APPLICATION_JSON + ";charset=utf-8").get(Word[].class
        );

        //add the received words and return new Article object
        Article result = new Article();
        for (int i = 0; i < words.length; i++) {
            result.add(words[i]);
        }
        return result;
    }

}
